package com.cn.jinl.service;


import com.cn.jinl.api.IProcureStorageOutService;
import com.cn.jinl.dao.ProcureStorageMapper;
import com.cn.jinl.dao.ProcureStorageOutHisMapper;
import com.cn.jinl.dao.ProcureStorageOutMapper;
import com.cn.jinl.domain.ProcureStorage;
import com.cn.jinl.domain.ProcureStorageOut;
import com.cn.jinl.domain.ProcureStorageOutHis;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.GenerateUUID;
import com.cn.jinl.utils.QueryForm;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

@Service
public class ProcureStorageOutServiceImpl implements IProcureStorageOutService {

    @Autowired
    private ProcureStorageOutMapper procureStorageOutMapper;

    @Autowired
    private ProcureStorageOutHisMapper procureStorageOutHisMapper;

    @Autowired
    private ProcureStorageMapper procureStorageMapper;

    @Autowired
    private GenerateUUID generateUUID;

    @Override
    public List<ProcureStorageOut> getProcureStorageOutList(QueryForm queryForm) throws Exception {
        int count = procureStorageOutMapper.selectProcureStorageOutListCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProcureStorageOut> procureStorageOuts = procureStorageOutMapper.selectProcureStorageOutList(queryForm);
        procureStorageOuts.get(0).setCount(count);
        return procureStorageOuts;
    }

    @Override
    public List<ProcureStorageOut> selectProcureStorageOutMXList(QueryForm queryForm) throws Exception {
        int count = procureStorageOutMapper.selectProcureStorageOutMXListCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProcureStorageOut> procureStorageOuts = procureStorageOutMapper.selectProcureStorageOutMXList(queryForm);
        procureStorageOuts.get(0).setCount(count);
        return procureStorageOuts;
    }

    @Override
    public List<ProcureStorageOutHis> getProcureStorageOutHisById(String id) throws Exception {
        return procureStorageOutHisMapper.selectProcureStorageOutHisById(id);
    }

    @Override
    public String addProcureStorageOut(ProcureStorageOut procureStorageOut) throws Exception {
        List<ProcureStorageOutHis> procureStorageOutHisList = procureStorageOut.getProcureStorageOutHisList();
        if(CollectionUtils.isEmpty(procureStorageOutHisList)){
            return "请添加出库物料！";
        }
        List<ProcureStorage> procureStorages = procureStorageMapper.selectProcureStorageDetailList(new QueryForm());
        if(CollectionUtils.isEmpty(procureStorages)){
            return "未查询到入库物料！";
        }

        List<ProcureStorageOutHis> hasOuts = procureStorageOutHisMapper.selectHasOutStorages();

        Map<String,Double> hasLeftMaps = Maps.newHashMap();
        Map<String,Double> hasInMaps = Maps.newHashMap();
        for(ProcureStorage procureStorage:procureStorages){
            double val = Double.parseDouble(procureStorage.getProcureExistCount());
            if(hasInMaps.containsKey(procureStorage.getMaterialId())){
                double total = hasInMaps.get(procureStorage.getMaterialId())+val;
                hasInMaps.put(procureStorage.getMaterialId(), total);
                hasLeftMaps.put(procureStorage.getMaterialId(), total);
            }else{
                hasInMaps.put(procureStorage.getMaterialId(), val);
                hasLeftMaps.put(procureStorage.getMaterialId(), val);
            }
        }

        for(ProcureStorageOutHis procureStorageOutHis:hasOuts){
            String materialId = procureStorageOutHis.getMaterialId();
            if(!hasInMaps.containsKey(materialId)){
                continue;
            }
            double leftCount = hasInMaps.get(materialId) - Double.parseDouble(procureStorageOutHis.getOutCount());
            hasLeftMaps.put(materialId,leftCount);
        }
        String id = CommonUtil.generateRandomNum("storageOut-");
        for(ProcureStorageOutHis his:procureStorageOutHisList){
            String materialId = his.getMaterialId();
            if(!hasLeftMaps.containsKey(materialId)||hasLeftMaps.get(materialId) < 0){
                return "物料："+his.getMaterialName()+" 库存不足！";
            }
            his.setOutId(id);
        }
        procureStorageOut.setId(id);
        String key = "C"+generateUUID.getYearMoth();
        int num = generateUUID.getNumberFlag(key);
        String code = key+generateUUID.getThreeNumStr(num);
        procureStorageOut.setStorageOutCode(code);
        int count = procureStorageOutMapper.insertSelective(procureStorageOut);
        if(count < 1){
            return "插入表异常！";
        }
        count = procureStorageOutHisMapper.batchInsertStorageOutHis(procureStorageOutHisList);
        if(count < 1){
            throw new Exception("批量插入表失败");
        }
        return "";
    }
}
